from selenium import webdriver
from datetime import datetime
import time
import random



def test(name, pwd,address):
    location = ['山西省太原市小店区坞城街道书香苑(体育路)', '山西省太原市小店区坞城街道山西财经大学3号楼', '山西省太原市小店区坞城街道山西财经大学1号楼',
                '山西省太原市小店区科学路47号山西财经大学坞城校区（北院）']

    a = random.randint(1, 7)
    temp = a / 10 + 36

    #服务器运行的必要参数（无界面运行浏览器）
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox') # 这个配置很重要

    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/usr/bin/chromedriver_linux64/chromedriver')    
    # 如果没有把chromedriver加入到PATH中，就需要指明路径
    # driver = webdriver.Chrome(r'C:\Users\Karben\Desktop/chromedriver.exe') 
    # 如果没有把chromedriver加入到PATH中，就需要指明路径

    # 操作这个对象.
    driver.get('https://oauth.yiban.cn/code/html?client_id=31ab54ff0a0882ad&redirect_uri=http://f.yiban.cn/iapp217213&state=https%253A%252F%252Fyb.sxjtwl.net%252Fapp%252Fyq%252F%2523%252Fsign')  # get方式访问百度.

    # 使用完, 记得关闭浏览器, 不然chromedriver.exe进程为一直在内存中.
    # 定位输入框
    input_box = driver.find_element_by_id('oauth_uname_w')
    input_box_1 = driver.find_element_by_id('oauth_upwd_w')
    try:
        # 输入内容：selenium
        input_box.send_keys(name)
        input_box_1.send_keys(pwd)
    except Exception as e:
        print('fail')
    try:
        button = driver.find_element_by_class_name('oauth_check_login')
        # 点击登录按钮
        button.click()
        print('成功登录')
        time.sleep(2)
    except Exception as e:
        print('fail登录')


    try:
        #获取到新跳转页面的html里面的元素
        time.sleep(2)
        windows = driver.window_handles
        driver.switch_to.window(windows[-1])
        


        # 获得页面中input标签的list
        inputs = driver.find_elements_by_tag_name("input")
        #通过xpath获取页面底部的元素
        textt = driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div/div[2]/div/div[4]/a').text
        if textt != '修 改':
            for input_ in inputs:
                if input_.get_attribute("type") == "number":
                    input_.clear()
                    input_.send_keys(str(temp))   #输入温度
                if input_.get_attribute("type") == "text":
                    input_.clear()
                    # index = random.randint(0, len(location) - 1)
                    input_.send_keys(address)   #输入地址
            inputs2 = driver.find_elements_by_class_name("el-radio__inner")
            inputs2[0].click()  # 点击健康
            time.sleep(2)
            button1 = driver.find_element_by_link_text('提 交')
            button1.click()
            time.sleep(4)
            driver.quit()
        else:
            xxname = driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div/div[2]/div/div[2]/div/p[2]').text
          
            driver.quit()
    except Exception as se:
        print(se)
    # print(station)






if __name__ == "__main__":
    print("进入主函数")
    #定义打卡时间
    s_time1 = '00:05:00'
    s_time2 = '02:05:00'
    s_time3 = '04:21:10'
    s_time4 = '09:02:00'
    
 
    s_time5 = '14:23:00'
    s_time6 = '02:05:00'
    s_time7 = '06:05:00'
    s_time8 = '15:05:00'
    #无限循环
    while True:
        #获取现在的时间
        nowtime = datetime.now().strftime("%H:%M:%S")
        if (s_time1 == nowtime):   
            test("17635795566","1651564165","山西省朔龙园") #宋
        
        if (s_time2 == nowtime)
            test("17635795566","1651564165","山西省朔龙园") #宋
            
        if (s_time3 == nowtime)
            test("17635795566","1651564165,"山西省朔园") #宋
         
            
        if (s_time4 == nowtime)
            test("17635795566","1651564165,"山西省朔州市山阴县俊秀龙园") #宋
           
   